import { _decorator, Component, director, EditBox, instantiate, log, Node, Prefab } from 'cc';
import { NetWorkManager } from '../Global/NetWorkManager';
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListRes } from '../Common';
import DataManager from '../Global/DataManager';
import { EventEnum, SceneEnum } from '../Enum';
import { PlayerManager } from '../UI/PlayerManager';
import { RoomManager } from '../UI/RoomManager';
import EventManager from '../Global/EventManager';
const { ccclass, property } = _decorator;

@ccclass('HallManager')
export class HallManager extends Component {
    @property(Node)
    playerContainer:Node

    @property(Prefab)
    playerPrefab:Prefab

    @property(Node)
    RoomContainer:Node

    @property(Prefab)
    RoomPrefab:Prefab

    protected onLoad(): void {
        //监听事件
        EventManager.Instance.on(EventEnum.RoomJoin,this.handleJoinRoom,this)
        //新玩家登录时老玩家重新渲染玩家列表
        NetWorkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList,this.renderPlayer,this)
        //新玩家登录时老玩家重新渲染房间列表
        NetWorkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList,this.renderRoom,this)
    }
    start(){
        
        this.playerContainer.destroyAllChildren()
        this.RoomContainer.destroyAllChildren()
        this.getPlayers()
        this.getRooms()
    }   
    onDestroy() {
        //解绑
        EventManager.Instance.off(EventEnum.RoomJoin,this.handleJoinRoom,this)
        NetWorkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList,this.renderPlayer,this)
        NetWorkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoomList,this.renderRoom,this)
    }
    /**
     * 获取玩家列表
     */
    async getPlayers(){
        //调用callApi方法
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList,{});
        if(!success){
            console.error(error)
            return
        }
        
        // console.log("res",res)
        this.renderPlayer(res)
    }
    /**
     *  渲染玩家列表
     */
    renderPlayer({list}:IApiPlayerListRes){
        for (const c of this.playerContainer.children) {
            c.active=false
        }

        while (this.playerContainer.children.length<list.length){
            const node = instantiate(this.playerPrefab)
            node.active=false
            node.setParent(this.playerContainer)
        }
        // console.log("list",list);
        
        for (let i = 0; i < list.length; i++) {
            const data = list[i];
            const node = this.playerContainer.children[i]
            node.getComponent(PlayerManager).init(data)
        }
    }

    /**
     * 点击创房
     */
    async handleCreateRoom(){
        //调用callApi方法
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate,{});
        if(!success){
            console.error(error)
            return
        }
        //保存返回的房间信息
        DataManager.Instance.roomInfo=res.room
        console.log("DataManager.Instance.roomInfo",DataManager.Instance.roomInfo);
        console.log("加入房间");
        
        director.loadScene(SceneEnum.Room)
        
    }

    /**
     * 获取房间列表
     */
    async getRooms(){
        //调用callApi方法
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiRoomList,{});
        if(!success){
            console.error(error)
            return
        }
        
        console.log("房间列表",res)
        this.renderRoom(res)
    }
     /**
     *  渲染房间列表
     */
     renderRoom({list}:IApiRoomListRes){
        for (const c of this.RoomContainer.children) {
            c.active=false
        }

        while (this.RoomContainer.children.length<list.length){
            const node = instantiate(this.RoomPrefab)
            node.active=false
            node.setParent(this.RoomContainer)
        }
        console.log("渲染房间列表",list);
        
        for (let i = 0; i < list.length; i++) {
            const data = list[i];
            const node = this.RoomContainer.children[i]
            node.getComponent(RoomManager).init(data)
        }
    }
    /**
     * 点击加入房间
     */
    async handleJoinRoom(rid:number){
        //调用callApi方法
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin,{
            rid,
        });
        if(!success){
            console.error(error)
            return
        }
        
        //保存返回的房间信息
        DataManager.Instance.roomInfo=res.room
        console.log("DataManager.Instance.roomInfo",DataManager.Instance.roomInfo);
        director.loadScene(SceneEnum.Room)
        
    }

}

